package com.csjbot.weedle_auto.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.csjbot.weedle_auto.R
import com.csjbot.weedle_auto.file.constants_util.FileUtil
import java.io.File
import java.util.*

/**
 * Created by chenqi on 8/9/17.
 */

class FolderAdapter(list: List<File>, private val mcontext: Context) : BaseAdapter() {
    private var list: List<File> = ArrayList()

    init {
        this.list = list
    }


    fun getList(list: List<File>) {
        this.list = list
    }

    override fun getCount(): Int {
        return list.size
    }

    override fun getItem(position: Int): Any {
        return list[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var viewHandler: ViewHandler
        //重用view
        var v: View
        if (convertView == null) {
            v = LayoutInflater.from(mcontext).inflate(R.layout.file_folder_text, null)
            viewHandler = ViewHandler()
            v.tag = viewHandler
            viewHandler.viewtext = v.findViewById(R.id.file_folder_text) as TextView
            viewHandler.viewtext2 = v.findViewById(R.id.file_folder_text2) as TextView
        } else {
            v = convertView
            viewHandler = v.tag as ViewHandler
        }
        if (count != 0) {
            viewHandler.viewtext!!.text = FileUtil.getName(list[position])
            viewHandler.viewtext2!!.text = FileUtil.getAllPath(list[position])
        } else {
            viewHandler.viewtext!!.text = "此文件夹没有值！"
        }
        return v
    }

    inner class ViewHandler {
        internal var viewtext: TextView? = null
        internal var viewtext2: TextView? = null
    }
}
